#include<bits/stdc++.h>
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define mid ((l+r)>>1)
#define File(xxx) freopen(xxx".in","r",stdin),freopen(xxx".out","w",stdout)
using namespace std;
namespace IO{
    char buff[1<<21],*p1=buff,*p2=buff;
    char getch(){
        return p1==p2&&(p2=((p1=buff)+fread(buff,1,1<<21,stdin)),p1==p2)?EOF:*p1++;
    }
    template<typename T>
    void read(T &x){
        char ch=getch();int fl=1;x=0;
        while(ch>'9'||ch<'0'){if(ch=='-')fl=-1;ch=getch();}
        while(ch<='9'&&ch>='0'){x=x*10+ch-48;ch=getch();}
        x*=fl;
    }
    template<typename T,typename ...Args>
    void read(T &x,Args& ...args){
        read(x);read(args...);
    }
    char obuf[1<<21],*p3=obuf;
    void putch(char ch){
        if(p3-obuf<(1<<21))*p3++=ch;
        else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=ch;
    }
    void pc(const char *s){
    	while(*s)putch(*s),s++;
    }
    char ch[100];
    template<typename T>
    void write(T x){
        if(!x)return putch('0');
        if(x<0)putch('-'),x*=-1;
        int top=0;
        while(x)ch[++top]=x%10+48,x/=10;
        while(top)putch(ch[top]),top--;
    }
    template<typename T,typename ...Args>
    void write(T x,Args ...args){
        write(x),putch(' '),write(args...);
    }
    void flush(){fwrite(obuf,p3-obuf,1,stdout);p3=obuf;}
}
using namespace IO;
ll f[31][2][2][2][2];
inline bool Get(int x,int y){return (x>>y)&1;}
void sol(){
	int l,r;
	read(l,r);
	memset(f,0,sizeof f);
	f[30][1][1][1][1]=1;
	for(int i=30;i;i--)
	for(int s00=0;s00<2;s00++)
	for(int s01=0;s01<2;s01++)
	for(int s10=0;s10<2;s10++)
	for(int s11=0;s11<2;s11++)
	if(f[i][s00][s01][s10][s11])
	for(int s0=0;s0<2;s0++)
	for(int s1=s0;s1<2;s1++){
		if(s00&&Get(l,i-1)==1&&s0==0)
			continue;
		if(s01&&Get(r,i-1)==0&&s0==1)
			continue;
		if(s10&&Get(l,i-1)==1&&s1==0)
			continue;
		if(s11&&Get(r,i-1)==0&&s1==1)
			continue;
		f[i-1][s00&(s0==Get(l,i-1))][s01&(s0==Get(r,i-1))][s10&(s1==Get(l,i-1))][s11&(s1==Get(r,i-1))]+=f[i][s00][s01][s10][s11];
	}
	ll ans=0;
	for(int s00=0;s00<2;s00++)
	for(int s01=0;s01<2;s01++)
	for(int s10=0;s10<2;s10++)
	for(int s11=0;s11<2;s11++)
		ans+=f[0][s00][s01][s10][s11];
	write(ans),putch('\n');
}
signed main(){
	File("comb");
	int T;read(T);
	while(T--)sol();
	return flush(),0;
}

